#include "StdAfx.h"
#include "LuaConsole.h"
#include<comutil.h>

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
lua_State* gMainLua = NULL;
IMPLEMENT_DYNAMIC(CLuaConsoleDlg, CConsoleDlg)

CLuaConsoleDlg::CLuaConsoleDlg(lua_State* pLua)
{
	gMainLua = pLua;
}

CLuaConsoleDlg::~CLuaConsoleDlg(void)
{
}

BEGIN_MESSAGE_MAP(CLuaConsoleDlg, CConsoleDlg)
END_MESSAGE_MAP()

void CLuaConsoleDlg::OnConsoleCommand( CString& strCmd )
{
	_bstr_t w2a(strCmd.GetBuffer());
	char* pCommand = (char*)w2a;
	int error = luaL_loadbuffer(gMainLua, pCommand, strlen(pCommand), 
		"line") || lua_pcall(gMainLua, 0, 0, 0); 
	if (error) { 
		_bstr_t a2w(lua_tostring(gMainLua, -1));
		GetConsole().AddColorLine((wchar_t*)a2w,RGB(0xff,0x10,0x10)); 
		lua_pop(gMainLua, 1);/* pop error message from the stack */
	} 
	strCmd.ReleaseBuffer();
}
																	 